//
//  HeaderScaleViewController.m
//  tableView头部视图缩放
//
//  Created by MyMac on 15/7/21.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//
#define HEADER_H 300
#import "HeaderScaleViewController.h"
#import "ScaleImageView.h"
#import "UINavigationBar+BackgroundColor.h"
#define NAVBAR_CHANGE_POINT 50

@interface HeaderScaleViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic ,strong )UITableView*myTableView;
@property(nonatomic,strong)ScaleImageView*scaleView;
@end

@implementation HeaderScaleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 5, 100, 30)];
    btn.backgroundColor=[UIColor redColor];
    [self.navigationController.navigationBar addSubview:btn];
    UIBarButtonItem*done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(Btn)];
    self.navigationItem.leftBarButtonItem=done;
    
    
    //self.navigationController.navigationBar.barTintColor=[UIColor blueColor];
#warning 要想试图隐藏，而不隐藏子试图，，不该设置视图的透明度，，而应该设置视图背景色的透明度
       [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor colorWithRed:100/255.0f green:130/255.0f blue:200/255.0f alpha:0]];
   
    //self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:100/255.0f green:130/255.0f blue:200/255.0f alpha:0];
    
    
    
    
    self.myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.myTableView.backgroundColor=[UIColor yellowColor];
    self.myTableView.bounces=YES;
    self.myTableView.dataSource=self;
    self.myTableView.delegate=self;
   self.myTableView.contentInset=UIEdgeInsetsMake(HEADER_H, 0, 0, 0);//表内容的范围，tableView里面有许多内容视图，内容视图在父视图中的位置
    //self.myTableView.contentOffset=CGPointMake(0, -300);//不能用偏移量计算
    self.automaticallyAdjustsScrollViewInsets=NO;//保证当前的偏移量为（0，0）
    [self.view addSubview:self.myTableView];
  
    
  _scaleView=[[ScaleImageView alloc]initWithFrame:CGRectMake(0,-HEADER_H, self.view.bounds.size.width, HEADER_H)];
   [self.myTableView addSubview:_scaleView];
  _scaleView.imageView.image=[UIImage imageNamed:@"11011917513f442e8c2bab707f.jpg"];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
 
#warning 去掉导航条透明后的边框效果
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];



}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


}
-(void)dealloc
{


}
-(void)Btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGPoint point=scrollView.contentOffset;
   // self.scaleView.backScroll.zoomScale=point.x/64;
    
    CGSize viewSize=self.scaleView.imageView.bounds.size;
    CGFloat scaley=(-point.y-HEADER_H)/viewSize.height;
     CGAffineTransform transform=self.scaleView.imageView.transform;
    NSLog(@"%@",NSStringFromCGPoint(point));
    if(point.y<=-HEADER_H){
        //[self.scaleView setTransform:CGAffineTransformMakeScale(1+scaley, 1+scaley)];//相对于最初始的位置设置
       [self.scaleView setTransform:CGAffineTransformScale(transform, 1+scaley, 1+scaley)];
  
    }
   //  NSLog(@"$$$$$$%@",NSStringFromCGRect(self.scaleView.frame));//当缩放时，，frame也是在跟着变

    if(point.y<=-HEADER_H){
    self.scaleView.center=CGPointMake(self.myTableView.center.x,- self.scaleView.frame.size.height/2.0);//缩放过程中，图片居中
    }
    
    
    UIColor * color = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1];
    CGFloat offsetY = scrollView.contentOffset.y;
//    if (offsetY > NAVBAR_CHANGE_POINT) {
//        CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
//        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
//    } else {
    
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:(offsetY+300)/64.0f]];
//    }
   // self.navigationController.navigationBar.barTintColor=[color colorWithAlphaComponent:(offsetY+300)/64.0f];
//    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:111/255.0f green:222/255.0f blue:223/255.0f alpha:point.y+300/64];

}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
  self.scaleView.imageView.transform=CGAffineTransformIdentity;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 70;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static    NSString*neme=@"1111";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:neme];
    if(cell==nil){
        
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:neme];
        cell.backgroundColor=[UIColor redColor];
    }
    static int i=0;
    cell.textLabel.text=[NSString stringWithFormat:@"%d",i++];
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
@end
